﻿#Requires AutoHotkey v2.0-a121
#SingleInstance Force
Persistent()

#Include <WebView2>

; DllCall("User32.dll\SetProcessDpiAwarenessContext", "Ptr", 0xFFFFFFFC) ; not sure if this is needed.  Works fine without it.

Array.prototype.DefineProp("Get", {Call: (this, idx) => this[idx + 1]})

main := gui("+Resize")
main.OnEvent("Close", ExitApp)

main.add("button", "vbtn", "Test")

; need to have the size also otherwise when you un-maximize it the window has zero height and width
wv := WebView2(main)
wv.OnControllerCompleted(handleControllerCreation)
wv.create()

main.Show(Format("w{} h{}", A_ScreenWidth * 0.4, A_ScreenHeight * 0.6))

OnMessage(WM_MOVE := 0x3, posChanged)
OnMessage(WM_MOVING := 0x216, posChanged)

OnMessage(WM_NCLBUTTONDOWN := 0xA1, ncClick)
Return

posChanged(*) {
    global wv
    
    wv.NotifyParentWinPosChanged()
}

ncClick(*) {
    global main
    ControlFocus(main)
}

handleControllerCreation(wv) {
    wv.parent.OnEvent("Size", mainSize.bind(wv))
    wv.put_Bounds()
    
    myObj := { stuff: true, things: (d) => MsgBox(Type(d)) }
    vbuf2 := BufferAlloc(24)
    myArrRef := ComObject(0x400C, vbuf2.ptr)  ; 0x400C is a combination of VT_BYREF and VT_VARIANT.
    myArr := ComObjArray(VT_VARIANT:=12, 2)
    myArr[0] := "one"
    myArr[1] := "two"
    myArrRef[] := myArr
    
    vbuf := BufferAlloc(24)
    vref := ComObject(0x400C, vbuf.ptr)  ; 0x400C is a combination of VT_BYREF and VT_VARIANT.
    
    arr := ComObjArray(VT_VARIANT:=12, 3)
    arr[0] := myObj
    arr[1] := ["Hot"]
    arr[2] := myArrRef
    
    vref[] := ["this", "is", "a", "test"] ; arr
    
    wv.AddHostObjectToScript("AHK", vref)
    
    ; token := wv.OnNavigationStarting(handleNavigationStarting)
    ; unsubscribe := wv.addLostFocus((*) => ToolTip("Focus lost"))
    ; unsubscribe2 := wv.addMoveFocusRequested((w, eventArgs) => ToolTip(eventArgs.reason))
    ; token2 := wv.OnNavigationCompleted((wv, eventArgs) => SetTimer((*) => wv.TrySuspend((success) => MsgBox(success)), -2000))
    
    wv.SetVirtualHostNameToFolderMapping("kczx3.example", "assets")
    ; wv.navigate("file:///" A_ScriptDir "/index.html")
    wv.navigate("https://kczx3.example/index.html")
}

handleNavigationStarting(wv, eventArgs) {
    isUserInitiated := eventArgs.isUserInitiated
    isRedirected := eventArgs.isRedirected
    
    if (isUserInitiated && !isRedirected) {
        uri := eventArgs.uri
        
        if (MsgBox("You are about to navigate to: " . uri . "`n`nDo you want to continue?", "Navigation warning", "YN Icon!") = "No") {
            
            eventArgs.Cancel := true
            
            ; ICoreWebView2::NavigateToString
            wv.navigateToString("<h1>Navigation Canceled</h1><p>You chose to cancel navigation to the following URL: " . uri . "</p>")
        }
    }
}

mainSize(wv, win, minMax, *) {
    ; for performance reasons, you should make the webview2 not visible when the parent window is minimized/hidden.
    isVisible := wv.isVisible
    if (minMax == -1) {
        if (isvisible) {
            wv.isVisible := false
        }
    }
    else {
        if (!isVisible) {
            wv.isVisible := true
        }
        else {
            wv.put_Bounds()
        }
        
    }
}

